<html>
<head><meta charset="utf-8"><title>validity of booleans · t-lang/wg-unsafe-code-guidelines · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/index.html">t-lang/wg-unsafe-code-guidelines</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html">validity of booleans</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="155025594"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025594" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025594">(Jan 13 2019 at 10:38)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> i think we agree</p>



<a name="155025600"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025600" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025600">(Jan 13 2019 at 10:39)</a>:</h4>
<p>when calling C functions rust functions would just need to adjust bools</p>



<a name="155025657"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025657" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025657">(Jan 13 2019 at 10:41)</a>:</h4>
<p>my point is that because calling convention compatibility is intended, we have to provide that as well, and that prevent us from saying anything about the values of true or false</p>



<a name="155025702"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025702" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025702">(Jan 13 2019 at 10:42)</a>:</h4>
<p>How booleans are encoded when passed in the calling convention is not observable for programs</p>



<a name="155025756"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025756" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025756">(Jan 13 2019 at 10:44)</a>:</h4>
<p>we can't use that information to say anything about the values of true and false, since C code can write <code>_Bool</code>s via a pointer into Rust memory expecting <code>bool</code>s</p>



<a name="155025768"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025768" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025768">(Jan 13 2019 at 10:45)</a>:</h4>
<p>When Rust and C (or C and C, or Rust and Rust) communicate through <em>memory</em>, then the memory representation is relevant and calling convention isn't</p>



<a name="155025769"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025769" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025769">(Jan 13 2019 at 10:45)</a>:</h4>
<p>and the memory representation of _Bool is unspecified</p>



<a name="155025770"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025770" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025770">(Jan 13 2019 at 10:46)</a>:</h4>
<p>while the calling convention is (I was hoping that we could use the calling convention to make the values of true and false implementation defined)</p>



<a name="155025813"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025813" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025813">(Jan 13 2019 at 10:46)</a>:</h4>
<p>Ok I see why you brought up calling conventions</p>



<a name="155025818"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025818" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025818">(Jan 13 2019 at 10:46)</a>:</h4>
<p>clang and gcc are slightly against specifying anything about the memory representation of _Bool</p>



<a name="155025821"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025821" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025821">(Jan 13 2019 at 10:47)</a>:</h4>
<p>while independently of whether that changes, they have to adhere to the calling convention</p>



<a name="155025828"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025828" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025828">(Jan 13 2019 at 10:47)</a>:</h4>
<p>But for the same reason you can have memory layout compatibility without calling convention capability, knowing how bools are passed around wouldn't give you memory layout compatibility</p>



<a name="155025829"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025829" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025829">(Jan 13 2019 at 10:47)</a>:</h4>
<p>And also, while the C standard is vague on matters of memory layout, it says even less (nothing at all, I believe) about calling conventions</p>



<a name="155025871"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025871" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025871">(Jan 13 2019 at 10:48)</a>:</h4>
<p>the C standard says nothing at all about calling conventions (the platform ABIs do say a lot though)</p>



<a name="155025876"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025876" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025876">(Jan 13 2019 at 10:48)</a>:</h4>
<p>And the platform ABIs also have to describe memory layout as well, don't they?</p>



<a name="155025877"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025877" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025877">(Jan 13 2019 at 10:48)</a>:</h4>
<p>the problem is that we'd like to have <code>bool</code>'s with two valid values, <code>true</code> and <code>false</code> - i don't see how that can work given the definition that <code>bool == _Bool</code></p>



<a name="155025886"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025886" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025886">(Jan 13 2019 at 10:49)</a>:</h4>
<p><span class="user-mention" data-user-id="124289">@rkruppe</span> i haven't checked, would make sense for IPC</p>



<a name="155025892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025892">(Jan 13 2019 at 10:49)</a>:</h4>
<p>Forget IPC, you need to be to share data structures in the same process as well</p>



<a name="155025893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025893">(Jan 13 2019 at 10:49)</a>:</h4>
<p>Certainly struct layout computation is part of the ABIs, so I don't see why primitive layout wouldn't be either</p>



<a name="155025940"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025940" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025940">(Jan 13 2019 at 10:50)</a>:</h4>
<blockquote>
<p>the problem is that we'd like to have <code>bool</code>'s with two valid values, <code>true</code> and <code>false</code> - i don't see how that can work given the definition that <code>bool == _Bool</code></p>
</blockquote>
<p>yeah this is a problem but one unrelated to calling conventions, do you agree?</p>



<a name="155025941"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025941" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025941">(Jan 13 2019 at 10:50)</a>:</h4>
<p>I can't find that for _Bool in the SysV AMD64 ABI spec</p>



<a name="155025944"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025944" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025944">(Jan 13 2019 at 10:50)</a>:</h4>
<p>i only find there the specification of bool for function arguments and return values</p>



<a name="155025957"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155025957" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155025957">(Jan 13 2019 at 10:51)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> C-the-standard has fairly strict definition for <code>_Bool</code> only allowing it to contain two distinct values.</p>



<a name="155026001"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026001" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026001">(Jan 13 2019 at 10:52)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> page 14 of <a href="https://software.intel.com/sites/default/files/article/402129/mpx-linux64-abi.pdf" target="_blank" title="https://software.intel.com/sites/default/files/article/402129/mpx-linux64-abi.pdf">https://software.intel.com/sites/default/files/article/402129/mpx-linux64-abi.pdf</a></p>
<blockquote>
<p>Booleans, when stored in a memory object, are stored as single byte objects the value of which is always 0 (false) or 1 (true).</p>
</blockquote>



<a name="155026008"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026008" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026008">(Jan 13 2019 at 10:53)</a>:</h4>
<p>I remember looking into making C compiler store something non-1 or non-0 into a <code>_Bool</code>. It was extremely difficult</p>



<a name="155026009"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026009" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026009">(Jan 13 2019 at 10:53)</a>:</h4>
<p>and ended up being UB in C as well.</p>



<a name="155026012"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026012" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026012">(Jan 13 2019 at 10:53)</a>:</h4>
<p>The C standard definition of _Bool is pretty loose, a lot of it is unspecified.</p>



<a name="155026057"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026057" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026057">(Jan 13 2019 at 10:54)</a>:</h4>
<p>a lot is, but what is specificed is fairly compatible with our own <code>bool</code>. Adding specifications from the ABI documents you get the complete picture.</p>



<a name="155026060"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026060" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026060">(Jan 13 2019 at 10:54)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> with the ABI documents you get the complete picture, which is that <code>bool</code> is pretty much implementation-defined</p>



<a name="155026068"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026068" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026068">(Jan 13 2019 at 10:55)</a>:</h4>
<p>Knowledge of specific C implementations is all good and well and I think it's quite likely that all the ones we care about in practice are compatible with what rustc does today; the real problem is hypothetical standard-conforming implementations that aren't</p>



<a name="155026071"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026071" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026071">(Jan 13 2019 at 10:55)</a>:</h4>
<p>But since these are very hypothetical I care very little so I'm going to peace out if/when this discussion turns to language lawyering again</p>



<a name="155026112"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026112" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026112">(Jan 13 2019 at 10:56)</a>:</h4>
<p>I disagree. <code>_Bool</code> is ABI-specific, but not implementation specific. Multiple implementors of the same ABI will implement <code>_Bool</code> the same way.</p>



<a name="155026114"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026114" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026114">(Jan 13 2019 at 10:56)</a>:</h4>
<p>I don't really see that as a meaningful distinction</p>



<a name="155026115"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026115" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026115">(Jan 13 2019 at 10:56)</a>:</h4>
<p>The issue is that saying that <code>bool</code> has two valid values, <code>true</code> and <code>false</code>, is incompatible with saying that <code>bool == _Bool</code>.</p>



<a name="155026117"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026117" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026117">(Jan 13 2019 at 10:56)</a>:</h4>
<p>Multiple implementations can and do agree with each other to get interoperability</p>



<a name="155026127"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026127" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026127">(Jan 13 2019 at 10:57)</a>:</h4>
<p>I'd be fine with saying that Rust does not support platforms in which <code>_Bool</code> has more than two valid values</p>



<a name="155026128"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026128" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026128">(Jan 13 2019 at 10:57)</a>:</h4>
<p>The C <em>standard</em> specifies that <code>_Bool</code> may contain two bit patterns, one for true and another for false. It is hidden in a fairly obscure manner, but it is there. (Oh and the patterns are not exactly specified IIRC, but that’s not too much of an issue)</p>



<a name="155026170"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026170" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026170">(Jan 13 2019 at 10:58)</a>:</h4>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> because of padding bits that's not true</p>



<a name="155026171"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026171" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026171">(Jan 13 2019 at 10:58)</a>:</h4>
<p><a href="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/63#issue-241483785" target="_blank" title="https://github.com/rust-rfcs/unsafe-code-guidelines/pull/63#issue-241483785">https://github.com/rust-rfcs/unsafe-code-guidelines/pull/63#issue-241483785</a></p>



<a name="155026228"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026228" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026228">(Jan 13 2019 at 11:00)</a>:</h4>
<blockquote>
<p><span class="user-mention" data-user-id="123586">@nagisa</span> because of padding bits that's not true</p>
</blockquote>
<p>are you saying an equivalent of <code>uint16_t</code> has more than 2¹⁶-1 bit patterns because of padding bits it may have coming after it?</p>



<a name="155026241"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026241" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026241">(Jan 13 2019 at 11:01)</a>:</h4>
<p>/me drops the topic.</p>



<a name="155026285"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026285" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026285">(Jan 13 2019 at 11:02)</a>:</h4>
<p>Are the fixed-width integer types allowed to have padding bits ?</p>



<a name="155026343"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026343" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026343">(Jan 13 2019 at 11:04)</a>:</h4>
<p>Sure, you can have data layout to specify alignment for integer larger than its native size.</p>



<a name="155026348"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026348" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> nagisa <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026348">(Jan 13 2019 at 11:04)</a>:</h4>
<p>I believe there is at least one architecture where 8-bit integer is actually a 16-bit one, with 8 padding bits.</p>



<a name="155026393"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026393" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026393">(Jan 13 2019 at 11:06)</a>:</h4>
<p>The whole point of padding bits is that they are ignored, so an <code>uint16_t</code> with ignored padding bits would have more than 2¹⁶-1 bit patterns because it would need to be larger than 16 bit</p>



<a name="155026408"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026408" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026408">(Jan 13 2019 at 11:06)</a>:</h4>
<p>the question is whether it has more than 2¹⁶-1 values, which it does not AFAICT - since the padding bits are ignored, different bit-patterns represent the same value</p>



<a name="155026417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155026417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155026417">(Jan 13 2019 at 11:07)</a>:</h4>
<p>that's the problem with a bool where the bit 0 encodes true and false, and e.g., bits 1-7 are ignored. It has 256 bit-patterns, but only two valid values, the problem is that multiple bit-patterns represent the same value</p>



<a name="155042775"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155042775" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155042775">(Jan 13 2019 at 19:18)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> why do we keep bringing this up</p>



<a name="155042776"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155042776" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155042776">(Jan 13 2019 at 19:18)</a>:</h4>
<p>zero platforms do anything but the sane thing</p>



<a name="155042780"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155042780" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155042780">(Jan 13 2019 at 19:18)</a>:</h4>
<p>until someone comes up with a platform, let's not care about it</p>



<a name="155067560"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155067560" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155067560">(Jan 14 2019 at 07:41)</a>:</h4>
<p><span class="user-mention" data-user-id="125253">@Nicole Mazzuca</span> because <span class="user-mention" data-user-id="120791">@RalfJ</span> would like to guarantee that <code>bool</code> has two valid values: <code>true</code> and <code>false</code>, but AFAICT that's incompatible with our definition that <code>bool == _Bool</code>, so we can't write the spec to be as simple as that</p>



<a name="155067621"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155067621" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155067621">(Jan 14 2019 at 07:43)</a>:</h4>
<p>We have to write that <code>bool</code> has as many valid values as <code>_Bool</code>, and add a "note: on all platforms that Rust currently support, <code>bool</code> only has two valid values, <code>true</code> (<code>0x1</code>) and <code>false</code> (<code>0x0</code>)", or similar</p>



<a name="155067743"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155067743" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155067743">(Jan 14 2019 at 07:47)</a>:</h4>
<p><code>_Bool</code> has only two valid values</p>



<a name="155067745"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155067745" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155067745">(Jan 14 2019 at 07:47)</a>:</h4>
<p>that is guaranteed by the C standard</p>



<a name="155067753"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155067753" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155067753">(Jan 14 2019 at 07:48)</a>:</h4>
<p>the representation is not guaranteed</p>



<a name="155067793"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155067793" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155067793">(Jan 14 2019 at 07:48)</a>:</h4>
<p>all zeroes is guaranteed to be <code>0</code> (aka <code>false</code>)</p>



<a name="155069315"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155069315" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155069315">(Jan 14 2019 at 08:26)</a>:</h4>
<p><span class="user-mention" data-user-id="125253">@Nicole Mazzuca</span> AFAICT <code>_Bool</code> has only one bit of valid values, and whether all other bits are trap representations or padding bits is unspecified. That is, a <code>_Bool</code> where one bit denotes <code>true</code> or <code>false</code>, and all other bits are ignored, is standard compliant AFAICT. That results in &gt; 2 bit-patterns that denote either <code>true</code> or <code>false</code>.</p>



<a name="155069405"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155069405" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155069405">(Jan 14 2019 at 08:28)</a>:</h4>
<p>that is not my understanding - however, I also think it's unreasonable to attempt to be compatible with anything that breaks this very basic assumption, since no system that Rust will work on does</p>



<a name="155069411"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155069411" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Nicole Mazzuca <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155069411">(Jan 14 2019 at 08:28)</a>:</h4>
<p>and no system which will be created in the future will either</p>



<a name="155069418"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155069418" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155069418">(Jan 14 2019 at 08:29)</a>:</h4>
<p>FWIW I filled a bug in both gcc and clang, and one possible solution for GCC is to offer an intrinsic that lets you query whether <code>_Bool</code> has padding bits</p>



<a name="155069427"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155069427" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155069427">(Jan 14 2019 at 08:29)</a>:</h4>
<p><a href="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88662" target="_blank" title="https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88662">https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88662</a></p>



<a name="155087492"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155087492" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155087492">(Jan 14 2019 at 14:28)</a>:</h4>
<p><span class="user-mention" data-user-id="120791">@RalfJ</span> C does have a notion of a validity invariant, it is just not the same one as Rust</p>



<a name="155087524"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155087524" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155087524">(Jan 14 2019 at 14:29)</a>:</h4>
<p>In C, an lvalue expression involving a value that is not "valid" is UB</p>



<a name="155087548"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/155087548" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#155087548">(Jan 14 2019 at 14:29)</a>:</h4>
<p>So while one can materialize a <code>_Bool</code> with an invalid bit-pattern, that <code>_Bool</code> cannot participate in an lvalue expression (e.g. an assignment)</p>



<a name="156599431"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/156599431" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#156599431">(Jan 22 2019 at 12:15)</a>:</h4>
<p><span class="user-mention" data-user-id="132920">@gnzlbg</span> </p>
<blockquote>
<p>In C, an lvalue expression involving a value that is not "valid" is UB</p>
</blockquote>
<p>can you point me at the part of the standard specifying this?</p>



<a name="156599483"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/156599483" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> RalfJ <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#156599483">(Jan 22 2019 at 12:16)</a>:</h4>
<p>I guess aside from <code>bool</code> there's little C would consider "invalid" so the question doesn't come up very often</p>



<a name="156604028"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity%20of%20booleans/near/156604028" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> gnzlbg <a href="https://rust-lang.github.io/zulip_archive/stream/136281-t-lang/wg-unsafe-code-guidelines/topic/validity.20of.20booleans.html#156604028">(Jan 22 2019 at 13:39)</a>:</h4>
<p>It comes from the definition of trap representation - <a href="http://port70.net/~nsz/c/c11/n1570.html#6.2.6.1p5" target="_blank" title="http://port70.net/~nsz/c/c11/n1570.html#6.2.6.1p5">http://port70.net/~nsz/c/c11/n1570.html#6.2.6.1p5</a> (emphasis mine)</p>
<blockquote>
<p>Certain object representations need not represent a value of the object type. <strong>If the stored value of an object has such a representation and is read by an lvalue expression that does not have character type, the behavior is undefined.</strong> If such a representation is produced by a side effect that modifies all or any part of the object by an lvalue expression that does not have character type, the behavior is undefined. 50) Such a representation is called a trap representation.</p>
</blockquote>
<p>And the footnote: <a href="http://port70.net/~nsz/c/c11/n1570.html#note50" target="_blank" title="http://port70.net/~nsz/c/c11/n1570.html#note50">http://port70.net/~nsz/c/c11/n1570.html#note50</a></p>
<blockquote>
<p>Thus, an automatic variable can be initialized to a trap representation without causing undefined behavior, but the value of the variable cannot be used until a proper value is stored in it.</p>
</blockquote>
<p>That is, one can construct an invalid value, but there is very little that one can do with that value without invoking undefined behavior (e.g. an assignment from an invalid value <code>bool a = invalid_bool</code> is UB - but reading and writing bytes to it via a <code>char*</code> is ok).</p>
<p>Note that trap representations are different from _indeterminate_ representations (which might represent a value of the object type - it is just unknown which).</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>